package martial.arts.tracker

class AttendanceController {

    static allowedMethods = [save: "POST", update: "POST", delete: "POST"]

    def index = {
        redirect(action: "list", params: params)
    }

    def list = {
        params.max = Math.min(params.max ? params.int('max') : 10, 100)
        [attendanceInstanceList: Attendance.list(params), attendanceInstanceTotal: Attendance.count()]
    }

    def create = {
        println "in create"
        println "----------- start session params -----------"
        params.each {k, v -> println k + "->" + v}
        println "----------- end session params -----------"
        println "in create"
        def attendanceInstance = new Attendance()
        attendanceInstance.properties = params

        params.each {k, v -> println k + "->" + v}

        return [attendanceInstance: attendanceInstance]
    }

    def search = {

        def searchStudentId = params.studentId
        def searchLastName = params.lastName
        println "search params: ${searchStudentId} ${searchLastName}"
        def searchStudentList = null
        if (searchStudentId) {
            println "searching by id"
            searchStudentList = Student.findAllByStudentId(searchStudentId)
        } else if (searchLastName) {
            println "searching by lastName"
            searchStudentList = Student.findAllByLastNameIlike("%${searchLastName}%")
        } else {
            //todo figure out errors
            println "no search params provided"
        }

        params.max = Math.min(params.max ? params.int('max') : 10, 100)
        searchStudentList.each {println it}
        params.each {k, v -> println k + "->" + v}
        render(template: '/templates/person_search_template', model: [studentInstanceList: searchStudentList, studentInstanceTotal: searchStudentList.size()])

    }

    def save = {
        println "getting into save!"
        println "----------- start session params -----------"
        params.each {k, v -> println k + "->" + v}
        println "----------- end session params -----------"
        def attendanceInstance = new Attendance(params)
        Student s = Student.findByStudentId(params.selectedStudent)
        attendanceInstance.setStudent(s)
        if (attendanceInstance.save(flush: true)) {
            flash.message = "${message(code: 'default.created.message', args: [message(code: 'attendance.label', default: 'Attendance'), attendanceInstance.id])}"
            redirect(action: "show", id: attendanceInstance.id)
        }
        else {
            println "4"
            render(view: "create", model: [attendanceInstance: attendanceInstance])
            println "3"
        }
    }

    def show = {
        def attendanceInstance = Attendance.get(params.id)
        if (!attendanceInstance) {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'attendance.label', default: 'Attendance'), params.id])}"
            redirect(action: "list")
        }
        else {
            [attendanceInstance: attendanceInstance]
        }
    }

    def edit = {
        def attendanceInstance = Attendance.get(params.id)
        if (!attendanceInstance) {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'attendance.label', default: 'Attendance'), params.id])}"
            redirect(action: "list")
        }
        else {
            return [attendanceInstance: attendanceInstance]
        }
    }

    def update = {
        def attendanceInstance = Attendance.get(params.id)
        if (attendanceInstance) {
            if (params.version) {
                def version = params.version.toLong()
                if (attendanceInstance.version > version) {

                    attendanceInstance.errors.rejectValue("version", "default.optimistic.locking.failure", [message(code: 'attendance.label', default: 'Attendance')] as Object[], "Another user has updated this Attendance while you were editing")
                    render(view: "edit", model: [attendanceInstance: attendanceInstance])
                    return
                }
            }
            attendanceInstance.properties = params
            if (!attendanceInstance.hasErrors() && attendanceInstance.save(flush: true)) {
                flash.message = "${message(code: 'default.updated.message', args: [message(code: 'attendance.label', default: 'Attendance'), attendanceInstance.id])}"
                redirect(action: "show", id: attendanceInstance.id)
            }
            else {
                render(view: "edit", model: [attendanceInstance: attendanceInstance])
            }
        }
        else {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'attendance.label', default: 'Attendance'), params.id])}"
            redirect(action: "list")
        }
    }

    def delete = {
        def attendanceInstance = Attendance.get(params.id)
        if (attendanceInstance) {
            try {
                attendanceInstance.delete(flush: true)
                flash.message = "${message(code: 'default.deleted.message', args: [message(code: 'attendance.label', default: 'Attendance'), params.id])}"
                redirect(action: "list")
            }
            catch (org.springframework.dao.DataIntegrityViolationException e) {
                flash.message = "${message(code: 'default.not.deleted.message', args: [message(code: 'attendance.label', default: 'Attendance'), params.id])}"
                redirect(action: "show", id: params.id)
            }
        }
        else {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'attendance.label', default: 'Attendance'), params.id])}"
            redirect(action: "list")
        }
    }
}
